#
# (C) Copyright 2006-2007
# Stefan Roese, DENX Software Engineering, sr@denx.de.
#
# (C) Copyright 2008
# Guennadi Liakhovetki, DENX Software Engineering, <lg@denx.de>
#
# See file CREDITS for list of people who contributed to this
# project.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA
#

CONFIG_MEM_POOL	= y
CONFIG_PRELOADER = y
CONFIG_SKIP_RELOCATE_UBOOT = y

include $(TOPDIR)/config.mk
include $(TOPDIR)/nand_mem_pool/board/$(BOARDDIR)/config.mk


LDSCRIPT= $(TOPDIR)/nand_mem_pool/board/$(BOARDDIR)/u-boot.lds
LDFLAGS	= -Bstatic -T $(nandobj)u-boot.lds -Ttext $(TEXT_BASE) $(PLATFORM_LDFLAGS)
AFLAGS	+= -DCONFIG_MEM_POOL
AFLAGS	+= -DCONFIG_PRELOADER
AFLAGS  += -DCONFIG_SKIP_RELOCATE_UBOOT
CFLAGS	+= -DCONFIG_MEM_POOL
CFLAGS	+= -DCONFIG_PRELOADER
CFLAGS  += -DCONFIG_SKIP_RELOCATE_UBOOT

SOBJS	= start.o cpu_init.o lowlevel_init.o reset.o ioinit.o mmu.o cache_flush.o

COBJS-y	+= boot_main.o
COBJS-y	+= timer.o
COBJS-y$(CONFIG_SYS_BOOT_NOR) += nand_boot.o
COBJS-y$(CONFIG_SYS_BOOT_NOR)	+= crc32.o
COBJS-y$(CONFIG_SYS_BOOT_NOR)	+= img_check.o
COBJS-y$(CONFIG_SYS_BOOT_NOR) += ix_nand.o
#COBJS-$(CONFIG_BOOT_AUTO_ADJUST) += denali.o
#COBJS-y$(CONFIG_DISABLE_BOOT_OTG)$(CONFIG_SYS_BOOT_NOR) += ix_udc.o

#COBJS	= nand_boot.o nand_ecc.o imapx200.o denali_test.o
COBJS	:= $(COBJS-y)

SRCS	:= $(addprefix $(obj),$(SOBJS:.o=.S) $(COBJS:.o=.c))
OBJS	:= $(addprefix $(obj),$(SOBJS) $(COBJS))
__OBJS	:= $(SOBJS) $(COBJS)
LNDIR	:= $(OBJTREE)/nand_mem_pool/board/$(BOARDDIR)

nandobj	:= $(OBJTREE)/nand_mem_pool/

ALL	= $(nandobj)u-boot-mem-pool $(nandobj)u-boot-mem-pool.bin $(nandobj)u-boot-mem-pool-16k.bin

all:	$(obj).depend $(ALL)

$(nandobj)u-boot-mem-pool-16k.bin: $(nandobj)u-boot-mem-pool
	$(OBJCOPY) ${OBJCFLAGS} --pad-to=$(PAD_TO) -O binary $< $@

$(nandobj)u-boot-mem-pool.bin:	$(nandobj)u-boot-mem-pool
	$(OBJCOPY) ${OBJCFLAGS} -O binary $< $@

$(nandobj)u-boot-mem-pool:	$(OBJS) $(nandobj)u-boot.lds
	cd $(LNDIR) && $(LD) $(LDFLAGS) $(__OBJS) \
		-Map $(nandobj)u-boot-mem-pool.map \
		-o $(nandobj)u-boot-mem-pool

$(nandobj)u-boot.lds: $(LDSCRIPT)
	$(CPP) $(CPPFLAGS) $(LDPPFLAGS) -ansi -D__ASSEMBLY__ -P - <$^ >$@

# create symbolic links for common files

# from cpu directory
$(obj)start.S:
	@rm -f $@
	@ln -s $(TOPDIR)/cpu/arm1136/start.S $@

# from SoC directory
$(obj)cpu_init.S:
	@rm -f $@
	@ln -s $(TOPDIR)/cpu/arm1136/imapx200/cpu_init.S $@

$(obj)reset.S:
	@rm -f $@
	@ln -s $(TOPDIR)/cpu/arm1136/imapx200/reset.S $@

$(obj)mmu.S:
	@rm -f $@
	@ln -s $(TOPDIR)/cpu/arm1136/imapx200/mmu.S $@

$(obj)cache_flush.S:
	@rm -f $@
	@ln -s $(TOPDIR)/cpu/arm1136/imapx200/cache_flush.S $@

$(obj)timer.c:
	@rm -f $@
	@ln -s $(TOPDIR)/cpu/arm1136/imapx200/timer.c $@

#$(obj)denali.c: $(obj)preloader.h
#	@rm -f $@
#	@ln -s $(TOPDIR)/cpu/arm1136/imapx200/denali.c $@

# from board directory
$(obj)lowlevel_init.S:
	@rm -f $@
	@ln -s $(TOPDIR)/board/infotm/imapx/lowlevel_init.S $@

# from drivers/mtd/nand directory
$(obj)ix_nand.c:
	@rm -f $@
	@ln -s $(TOPDIR)/drivers/mtd/nand/imapx200.c $@

$(obj)ix_udc.c:
	@rm -f $@
	@ln -s $(TOPDIR)/oem/udc.c $@

ifneq ($(OBJTREE),$(SRCTREE))

$(obj)preloader.h:
	@rm -f $@
	@ln -s $(src)preloader.h $@

$(obj)boot_main.c: $(obj)preloader.h
	@rm -f $@
	@ln -s $(src)boot_main.c $@

$(obj)crc32.c:
	@rm -f $@
	@ln -s $(src)crc32.c $@

$(obj)img_check.c:
	@rm -f $@
	@ln -s $(src)img_check.c $@

$(obj)nand_boot.c:
	@rm -f $@
	@ln -s $(src)nand_boot.c $@

$(obj)ioinit.S:
	@rm -f $@
	@ln -s $(src)ioinit.S $@
endif

#########################################################################

$(obj)%.o:	$(obj)%.S
	$(CC) $(AFLAGS) -c -o $@ $<

$(obj)%.o:	$(obj)%.c
	$(CC) $(CFLAGS) -c -o $@ $<

# defines $(obj).depend target
include $(SRCTREE)/rules.mk

sinclude $(obj).depend

#########################################################################
